package com.mxx.bu_yang.dialog;

import android.graphics.Rect;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.mxx.bu_yang.R;
import com.mxx.bu_yang.adapter.LocationHintAdapter;
import com.mxx.bu_yang.response.LocationHintResponse;
import com.mxx.common.config.ConstantsConfig;
import com.yp.loading.base.BaseDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：刘江飞
 * 时间：2020/10/15  9:03
 * 邮箱：642371742Z@qq.com
 * 描述：库位提醒弹窗
 **/
public class LocationHintDialog extends BaseDialog<LocationHintResponse>  implements View.OnClickListener {
    private TextView tv_close;
    private LocationHintAdapter mAdapter;
    private final Map<String, String> paramsMap = new HashMap<>();

    @Override
    public boolean isCancelable() {
        return false;
    }

    @Override
    protected boolean isCancelOutside() {
        return false;
    }

    /**
     * Sets layout id.
     *
     * @return the layout id
     */
    @Override
    protected int setLayoutId() {
        return R.layout.dialog_location_hint;
    }

    @Override
    protected void initView() {
        super.initView();
        RecyclerView rv_list = mRootView.findViewById(R.id.rv_list);
        tv_close = mRootView.findViewById(R.id.tv_close);
        if (rv_list.getItemDecorationCount() == 0) {
            RecyclerView.ItemDecoration decoration = new RecyclerView.ItemDecoration() {
                @Override
                public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                    super.getItemOffsets(outRect, view, parent, state);
                    int mode = parent.getChildAdapterPosition(view);
                    outRect.left = 10;
                    outRect.right = 10;
                    if (mode == 0) {
                        outRect.top = 0;
                    } else {
                        outRect.top = 14;
                    }
                }
            };
            rv_list.addItemDecoration(decoration);
        }
        LinearLayoutManager mManager = new LinearLayoutManager(mActivity);
        mManager.setOrientation(RecyclerView.VERTICAL);
        rv_list.setLayoutManager(mManager);
        mAdapter = new LocationHintAdapter(mActivity);
        rv_list.setAdapter(mAdapter);
        String value = (String) getSerializableValue(ConstantsConfig.VALUE);
        paramsMap.put("value", value);
    }

    @Override
    protected void initData() {
        super.initData();
        loadWarehouseInfo();
    }

    private void loadWarehouseInfo() {
        try {
            String value = paramsMap.get("value");
            List<LocationHintResponse> list = new Gson().fromJson(value, new TypeToken<List<LocationHintResponse>>() {
            }.getType());
            if (list != null && !list.isEmpty()) {
                List<LocationHintResponse> list1 = new ArrayList<>();
                for (LocationHintResponse response : list) {
                    LocationHintResponse hintResponse;
                    hintResponse = new LocationHintResponse();
                    hintResponse.item_type = 0;
                    hintResponse.order_num = response.order_num;
                    list1.add(hintResponse);
                    hintResponse = new LocationHintResponse();
                    hintResponse.item_type = 1;
                    hintResponse.warehouse_name = response.warehouse_name;
                    hintResponse.actual = response.actual;
                    list1.add(hintResponse);
                }
                mAdapter.setList(list1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @Override
    protected void setListener() {
        super.setListener();
        tv_close.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_close) {
            if (mResultListener == null) {
                dismiss();
                return;
            }
            dismiss();
            mResultListener.result(null);
        }
    }
}
